﻿var user;
var transferId;
$(function () {
    transferId = $("#hdnTransferId").val();

    $.ajax({
        url: "/Personnel/EditNurseTransfer/GetLoginUserInfo?ram" + Math.random(),
        type: "post",
        async: false,
        success: function (r) {
            user = $.parseJSON(r);
        }
    });

    $("#btnBack").click(function () {
        if ($("#hdnPageType").val() == 0) {
            location.href = "/Personnel/NurseTransferList";
        }
        else {
            location.href = "/Personnel/NurseTransferReview";
        }
    });

    bindInDepartment();

    bindNurseTransfer();
});

function bindNurseTransfer() {
    $.ajax({
        url: "/Personnel/EditNurseTransfer/GetNurseTransferInfo?ram" + Math.random(),
        type: "post",
        data: { id: transferId },
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                var nurseTransfer = d.Body;
                if (nurseTransfer.Status != $("#hdnStatus").val() && transferId != 0) {
                    $.ligerDialog.error("申请状态已改变！");
                    setTimeout(function () { $("#btnBack").click(); }, 1000);
                    return;
                }

                $(".reviewInfo").show();

                transferId = nurseTransfer.Id;
                $("#hdnTransferId").val(transferId);
                $("#fontName").text(nurseTransfer.UserName);
                $("#fontDepartmentName").text(nurseTransfer.DepartmentName);
                $("#fontReviewReason").text(nurseTransfer.ReviewReason);
                if (nurseTransfer.ReviewReason == null) {
                    $("[tab='reviewReason']").hide();
                }
                $("#fontCreateTime").text(nurseTransfer.CreateTime);
                $("#txtRemarks").val(nurseTransfer.Remarks);
                $("#inDepartmentCode").ligerComboBox().setValue(nurseTransfer.InDepartmentCode);
                $("#inDepartmentCode").blur();

                if ($("#hdnPageType").val() == 0) {
                    if (nurseTransfer.Status == 0) {
                        $("#fontStatus").text("未审核");
                        $("#btn1").val("保存").show().click(function () { save(0) });
                    }
                    if (nurseTransfer.Status == 1) {
                        $("#fontStatus").text("已通过");
                        $("#btnAdd").show();
                    }
                    if (nurseTransfer.Status == 2) {
                        $("#fontStatus").text("已驳回");
                        $("#btn1").val("保存").show().click(function () { save(0) });
                    }
                }
                else {
                    if (nurseTransfer.Status == 0) {
                        $("#fontStatus").text("未审核");
                        if (user.JobTitle == "护士长") {
                            $("#btn1").val("通过").show().click(function () { review(1) });
                            $("#btn2").val("不通过").show().click(function () { review(2) });
                        }
                    }
                    if (nurseTransfer.Status == 1) {
                        $("#fontStatus").text("已通过");
                    }
                    if (nurseTransfer.Status == 2) {
                        $("#fontStatus").text("已驳回");
                    }
                }
            }
            else if (d.Result == 0) {
                if (d.Body != "未找到对应数据!") {
                    $.ligerDialog.error(d.Body);
                }
                else {
                    $("#fontName").text(user.Name);
                    $("#fontDepartmentName").text(user.CurrDepName);
                    $("#btn1").val("保存").show().click(function () { save(0) });
                }
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

function bindInDepartment() {
    $("#inDepartmentCode").ligerComboBox({
        width: 120,
        cancelable: false,
        url: "/Personnel/EditNurseTransfer/GetAllUnits?isAll=1&ram" + Math.random(),
        valueField: "DepartmentCode",
        textField: "DepartmentName",
        valueFieldID: "hdnInDepartmentCode",
        value: -1,
    });
}

function save(status) {
    var inDepartmentCode = $("#hdnInDepartmentCode").val();
    var remarks = $("#txtRemarks").val();

    if (inDepartmentCode == "-1") {
        $.ligerDialog.error("转科病区不能为空");
        $("#inDepartmentCode").focus();
        return;
    }

    if (IsEmpty(remarks)) {
        $.ligerDialog.error("申请理由不能为空");
        $("#txtRemarks").focus();
        return;
    }

    $.ajax({
        url: "/Personnel/EditNurseTransfer/Save",
        type: "post",
        data: {
            Id: transferId,
            InDepartmentCode: inDepartmentCode,
            Remarks: remarks,
            Status: status
        },
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                transferId = d.Body.Id;
                $("#hdnTransferId").val(transferId);
                $("#fontStatus").text("未审核");
                $.ligerDialog.success("保存成功");
                setTimeout(function () { $("#btnBack").click(); }, 1000);
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

function review(status) {
    $.ligerDialog.open({
        height: 300,
        width: 400,
        title: '请填写审核理由',
        url: '/Personnel/NurseTransferReview/Review',
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: { id: transferId, status: status },
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnBack").click();
            }

        }]
    });
    $(".l-dialog-buttons").hide();
}
